<?php
namespace app\admin\controller;
use app\model\ImportTask as ImportTaskModel;
use think\Db;


class ImportTask extends Base
{
    public function __construct(){
        $model = new ImportTaskModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '导入任务';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $model = $this->model;
        $fields = [
            'name'=>['name'=>'分支机构名称','type'=>'text','valids'=>['required']],
            'address'=>['name'=>'地址','type'=>'text'],
            'tel'=>['name'=>'办公电话','type'=>'text'],
            'fax'=>['name'=>'传真','type'=>'text'],
            'contacts'=>['name'=>'联系人','type'=>'text'],
            'email'=>['name'=>'邮箱','type'=>'text']
        ];
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);

        $this->page_info['view'] = 'save';
    }

    public function assignSearchs(){
        $model = $this->model;
        $search_params = [
            [
                ['key'=>'id','name'=>'任务ID','type'=>'text'],
                ['key'=>'status','name'=>'状态','type'=>'select1_1','params'=>$model->getStatusList()],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        set_time_limit ( 0 );
        ini_set('memory_limit','2048M');
        
        $model = $this->model;
        $map_params = [
            ['key'=>'id','type'=>'='],
            ['key'=>'status','type'=>'='],
        ];
        $map = getSearchParam($map_params);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function cancel($id){
        $model = $this->model;
        $data = $model->where('id',$id)->find();
        if ($data['status'] != 0) {
            $this->error('任务状态不符');
        }

        if ( $model->where('id',$id)->update([
            'status'=>-1,
            'utime'=>time()
        ]) !== false ) {
            $this->success("取消成功");
        }else{
            $this->error("取消失败");
        }
    }

    public function read(){
        $import_task_id = input('import_task_id');
        $model = $this->model;
        $data = $model->where('id',$import_task_id)->find();

        $search_params = [
            [
                ['key'=>'import_task_id','name'=>'任务ID','type'=>'text'],
                ['key'=>'status','name'=>'状态','type'=>'select1_1','params'=>['失败','成功']],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];

        $map_params = [
            ['key'=>'import_task_id','type'=>'='],
            ['key'=>'status','type'=>'='],
        ];
        $map = getSearchParam($map_params);

        $list = Db::name('import_item')->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            
        }
        return view('read', [
            'data'=>$data,
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function export(){
        $model = new ImportTaskModel();
        $map_params = [
            ['key'=>'import_task_id','type'=>'='],
            ['key'=>'status','type'=>'='],
        ];
        $map = getSearchParam($map_params);

        $list = Db::name('import_item')->where($map)->order('id','desc')->select();
        foreach ($list as $k => $vo) {
       
        }
        $data = [];
        $title = ['项目名称','状态','类型','备注'];
        $data[] = $title;
        foreach ($list as $k => $vo) {
            $tmp = [
                $vo['name'],
                $vo['status'] == 1 ? "成功" : "失败",
                $model->getImportTypeCn($vo['type']),
                $vo['msg']
            ];
            $data[] = $tmp;
        }
        // exportCsv(date('YmdHis'),$title,$data);
        $result = downloadFile(uuidCreate().'.xlsx',$data);
        if ($result['status'] == 0) {
            $this->error($result['msg']);
        }
    }
   
}
